const mongoose=require('mongoose');
const Comment=require('../models/comment.model');

//增 
exports.create=function(req,res,next){
    const comment=new Comment(req.body);
    comment.save().then(data=>{
        res.json(data);
    })
}
//修
exports.update=function(req,res,next){
    //两种不同的参数传递方式
    const id=req.params.id;
    const comment=new Comment(req.body);
    Comment.findByIdAndUpdate(id,{$set:req.body},{new:false})
    .then(data=>{
        res.json(data);
    })

}
//删除
exports.remove=function(req,res,next){
    const id=req.params.id;
    Comment.findByIdAndRemove(id,function(){
        res.json({'message':'数据已删除'})
    })
}
//删除多个

exports.removes=function(req,res,next){
    var ids=req.body.ids.split(',');;
    if(ids.length>0){
        Comment.remove({_id:{$in:ids}}).then(data=>{
            res.json({'message':'多条数据已删除'})
        })
    }else{
        res.status(404).send({'message':'404'});
    }
}

exports.list=function(req,res,next){
//  var newsId=req.params.newsId||0;
//  var page=(req.query.page)?req.query.page:1;
//  var limit=(req.query.rows)?req.query.rows:8;
//  var queryCondition={};
//  Comment.find({newsId:newsId},function(err,data){
//  	console.log(data);
//      queryCondition=data;
//  })
//  Comment.paginate(queryCondition,{page:+page,limit:+limit},function(err,result){
//      result.rows=result.docs;
//      delete result.docs;
//      res.json(result);
//  })
	var page=(req.query.page)?req.query.page:1;
    var limit=(req.query.rows)?req.query.rows:5;
    var queryCondition={};
    if(req.params.newsId&&req.params.newsId.trim().length>0){
        newsId=req.params.newsId;
        queryCondition={
            "newsId":newsId
        }
        
    }
    Comment.paginate(queryCondition,{page:+page,limit:+limit},function(err,result){
        result.rows=result.docs;
        delete result.docs;
        res.json(result);
    })
}